﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Web;
using System.Xml;

namespace DragonSolution.Web.Common.Common
{
    public class XmlUtil
    {
        private XmlDocument xmlDoc;
        private XmlNode xmlnode;
        private XmlElement xmlelem;

        /// <summary>
        /// 构造方法，创建XML操作对象
        /// </summary>
        /// <param name="filePath">xml相对路径</param>
        public XmlUtil(string filePath)
        {
            xmlDoc = new XmlDocument();
            OpenXML(GetMapPath(filePath));
        }
        /// <summary>
        /// 创建XML操作对象
        /// </summary>
        /// <param name="filePath">xml相对路径</param>
        public void OpenXML(string filePath)
        {
            try
            {
                if (!string.IsNullOrEmpty(filePath))
                {                    
                    xmlDoc.Load(filePath);
                }
            }
            catch (Exception ex)
            {
            }
        }

        /// <summary>  
        /// 创建一个带有根节点的Xml 文件  
        /// </summary>  
        /// <param name="fileName">文件名称</param>  
        /// <param name="rootName">根节点名称</param>  
        /// <param name="encode">编码方式:gb2312，UTF-8 等常见的</param>  
        /// <param name="savePath">保存的目录路径</param>  
        /// <returns></returns>  
        public bool CreatexmlDocument(string fileName, string rootName, string encode,string savePath)
        {
            try
            {
                string path= GetMapPath(savePath);
                XmlDeclaration xmldecl;
                XmlElement xmlelem;
                xmldecl = xmlDoc.CreateXmlDeclaration("1.0", encode, null);
                xmlDoc.AppendChild(xmldecl);
                xmlelem = xmlDoc.CreateElement("", rootName, "");
                xmlDoc.AppendChild(xmlelem);
                xmlDoc.Save(savePath+ fileName);
                return true;
            }
            catch (Exception e)
            {
                return false;
            }
        }

        /// <summary>  
        /// 获取指定节点的指定属性值  
        /// </summary>  
        /// <param name="nodeName">节点名称</param>  
        /// <param name="attributeName">此节点的属性名称</param>  
        /// <returns></returns>  
        public string GetXmlAttributeValue(string nodeName, string attributeName)
        {
            string strReturn = "";
            try
            {
                //根据指定路径获取节点  
                XmlNode xmlNode = xmlDoc.SelectSingleNode(nodeName);
                //获取节点的属性，并循环取出需要的属性值  
                XmlAttributeCollection xmlAttr = xmlNode.Attributes;
                for (int i = 0; i < xmlAttr.Count; i++)
                {
                    if (xmlAttr.Item(i).Name == attributeName)
                    {
                        strReturn = xmlAttr.Item(i).Value;
                    }
                }
            }
            catch (XmlException xmle)
            {
                throw xmle;
            }
            return strReturn;
        }

        /// <summary>  
        /// 获取指定节点的指定属性值  
        /// </summary>  
        /// <param name="node">节点</param>  
        /// <param name="attributeName">此节点的属性名称</param>  
        /// <returns></returns>  
        public string GetAttributeValue(XmlNode node, string attributeName)
        {
            string strReturn = "";
            try
            {
                if (node != null)
                {
                    //获取节点的属性，并循环取出需要的属性值  
                    XmlAttributeCollection xmlAttr = node.Attributes;
                    for (int i = 0; i < xmlAttr.Count; i++)
                    {
                        if (xmlAttr.Item(i).Name == attributeName)
                        {
                            strReturn = xmlAttr.Item(i).Value;
                        }
                    }
                }
            }
            catch (XmlException xmle)
            {
                throw xmle;
            }
            return strReturn;
        }

        /// <summary>  
        /// 获取指定节点的值  
        /// </summary>  
        /// <param name="nodeName">节点名称</param>  
        /// <returns></returns>  
        public string GetXmlNodeValue(string nodeName)
        {
            string strReturn = "";
            try
            {
                //根据路径获取节点  
                XmlNode xmlNode = xmlDoc.SelectSingleNode(nodeName);
                strReturn = xmlNode.InnerText;
            }
            catch (XmlException xmle)
            {
                throw xmle;
            }
            return strReturn;
        }

        /// <summary>
        /// 获取子节点集合
        /// </summary>
        /// <param name="nodeName">节点名</param>
        /// <returns></returns>
        public XmlNodeList GetChileNode(string nodeName)
        {
            XmlNode xn = xmlDoc.SelectSingleNode(nodeName);//获取节点
            if (xn != null)
            {
                XmlNodeList xnl = xn.ChildNodes; //获取节点列表
                return xnl;
            }
            else
                return null;
        }

        /// <summary>
        /// 获取根节点的子节点集合
        /// </summary>
        /// <returns></returns>
        public XmlNodeList GetRootChildNode()
        {
            XmlNode rootNode = xmlDoc.DocumentElement;            
            if (rootNode != null)
            {
                XmlNodeList list = rootNode.ChildNodes;
                return list;
            }
            else
                return null;
        }

        /// <summary>
        /// 获取物理路径
        /// </summary>
        /// <param name="filePath">相对路径</param>
        /// <returns>物理路径</returns>
        private string GetMapPath(string filePath)
        {
            if (!string.IsNullOrEmpty(filePath))
            {
                string path = HttpContext.Current.Server.MapPath(filePath);
                return path;
            }
            else
                return "";
        }





        //……
        //还有很多方法，以后用到再添加
    }
}
